var express = require('express');
var router   = express.Router();
var dataCtrl = require('../controllers/user.controller');
var jwt      = require('jsonwebtoken');


function verifyToken(req,res,next){
  const bearerHeader = req.headers['authorization'];
  if(typeof bearerHeader !== 'undefined'){
    const bearer      = bearerHeader.split(' ');
    const bearerToken = bearer[1];

    jwt.verify(bearerToken, 'mySecretString', function(err, decoded) {
      // err
      // decoded undefined
      if(decoded !== undefined){
        next();
      }else{
        res.sendStatus(403);
      }

    });



  }else{
    res.sendStatus(403);
  }


}

router.post('/', dataCtrl.create);
router.put('/:id', dataCtrl.update);
router.delete('/:id', dataCtrl.remove);
router.post('/list',verifyToken, dataCtrl.list);
router.get('/:id', dataCtrl.get);
router.post('/removes', dataCtrl.removes);
router.post('/login',dataCtrl.login)
module.exports = router;


/*
  http:   //localhost:3000/users         post
  http:   //localhost:3000/users/123     put
  http:   //localhost:3000/users/123     delete
  http:   //localhost:3000/users/123     get
  http:   //localhost:3000/users/list
*/